﻿using MxWeiXinPF.Common;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using MxWeiXinPF.WeiXinComm;
using Senparc.Weixin.MP.CommonAPIs;
using Senparc.Weixin.Entities;

namespace MxWeiXinPF.Web.admin.users
{
    public partial class userbbsdetail_edit : MxWeiXinPF.Web.UI.ManagePage
    {
        private string action = MXEnums.ActionEnum.Add.ToString();
        protected MxWeiXinPF.Model.manager admin_info = null;
        private int id = 0;
        protected int shopid = 0;
        private WeiXinCRMComm cpp = new WeiXinCRMComm();

        private void BindDate(int id)
        {
            string str = "../../upload/Default.jpg";
            DataSet bBSDS = new MxWeiXinPF.BLL.userbbs().GetBBSDS(id);
            StringBuilder builder = new StringBuilder();
            for (int i = 0; i < bBSDS.Tables[0].Rows.Count; i++)
            {
                if (!(bBSDS.Tables[0].Rows[i]["imgurl"].ToString().Equals("") || (bBSDS.Tables[0].Rows[i]["imgurl"].ToString() == null)))
                {
                    str = bBSDS.Tables[0].Rows[i]["imgurl"].ToString();
                }
                else
                {
                    str = "../../upload/Default.jpg";
                }
                if (i == 0)
                {
                    builder.AppendFormat("<table width=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\0\" class=\"table1\">", new object[0]);
                    builder.AppendFormat("<tr><td style=\"width:70px;\"><div class=\"d_author\"><div class=\"yd_img\"><img src=\"" + str + "\" width=\"60\" height=\"60\" /></div>", new object[0]);
                    builder.AppendFormat("<a class=\"yd_name\">" + bBSDS.Tables[0].Rows[i]["username"].ToString() + "</a></div></td>", new object[0]);
                    builder.AppendFormat("<td style=\"width:100%;\"><div class=\"tip\"><p>" + bBSDS.Tables[0].Rows[i]["Remarks"].ToString() + "</p>", new object[0]);
                    builder.AppendFormat("<p class=\"hf_time\">" + bBSDS.Tables[0].Rows[i]["updatetime"].ToString() + "</p></div></td></tr></table>", new object[0]);
                }
                else if (bBSDS.Tables[0].Rows[i]["username"].ToString() == bBSDS.Tables[0].Rows[0]["username"].ToString())
                {
                    builder.AppendFormat("<table width=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\0\" class=\"table1\"><tr><td style=\"width:70px;\">", new object[0]);
                    builder.AppendFormat("<div class=\"d_author\"><div class=\"yd_img\"><img src=\"" + str + "\" width=\"60\" height=\"60\" /></div>", new object[0]);
                    builder.AppendFormat("<a class=\"yd_name\">" + bBSDS.Tables[0].Rows[i]["username"].ToString() + "</a></div></td>", new object[0]);
                    builder.AppendFormat("<td style=\"width:100%;\"><div class=\"tip\"><p>" + bBSDS.Tables[0].Rows[i]["Remarks"].ToString() + "</p>", new object[0]);
                    builder.AppendFormat("<p class=\"hf_time\">" + bBSDS.Tables[0].Rows[i]["updatetime"].ToString() + "</p></div></td></tr></table>", new object[0]);
                }
                else
                {
                    builder.AppendFormat("<table width=\"0\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\" class=\"table1\"><tr><td style=\"width:100%;\">", new object[0]);
                    builder.AppendFormat("<div class=\"tip2\">" + bBSDS.Tables[0].Rows[i]["Remarks"].ToString() + "<div class=\"hf_time\">" + bBSDS.Tables[0].Rows[i]["updatetime"].ToString() + "</div></div></td>", new object[0]);
                    builder.AppendFormat("<td style=\"width:70px;\"><div class=\"d_author\"><div class=\"yd_img\"><img src=\"" + str + "\" width=\"60\" height=\"60\" /></div>", new object[0]);
                    builder.AppendFormat("<a class=\"yd_name\" >" + bBSDS.Tables[0].Rows[i]["username"].ToString() + "</a></div></td></tr></table>", new object[0]);
                }
            }
            this.divContent.InnerHtml = builder.ToString();
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            try
            {
                if (string.IsNullOrEmpty(this.tbRemarks.Value))
                {
                    base.JscriptMsg("留言内容不能为空", "", "Success");
                }
                else
                {
                    MxWeiXinPF.BLL.userbbs userbbs = new MxWeiXinPF.BLL.userbbs();
                    if (userbbs.AddDetail(this.id, this.tbRemarks.Value, 0, "", this.shopid) > 0)
                    {
                        string error = string.Empty;
                        string accessTokenOrAppId = this.cpp.getAccessToken(39, out error);
                        if (error != "")
                        {
                            return;
                        }
                        // Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendText(accessTokenOrAppId,
                        this.tbRemarks.Value = "";
                        this.BindDate(this.id);
                        base.JscriptMsg("提交成功", "", "Success");
                        SendTemplateMsg("",id);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string queryString = MXRequest.GetQueryString("action");
                if (!string.IsNullOrEmpty(queryString) && (queryString == MXEnums.ActionEnum.Edit.ToString()))
                {
                    this.action = MXEnums.ActionEnum.Edit.ToString();
                    this.id = MXRequest.GetQueryInt("id");
                    if (this.id == 0)
                    {
                        base.JscriptMsg("传输参数不正确！", "back", "Error");
                        return;
                    }
                }
                this.admin_info = base.GetAdminInfo();
                this.shopid = this.admin_info.id;
                if (!this.Page.IsPostBack)
                {
                    base.ChkAdminLevel("userbbs_list", MXEnums.ActionEnum.View.ToString());
                    if (this.action == MXEnums.ActionEnum.Edit.ToString())
                    {
                        this.BindDate(this.id);
                    }
                }
            }
            catch (Exception ex)
            {

            }
        }

        private bool SendTemplateMsg(string content, int id)
        {
            try
            {
                Model.userbbs bbsmodel = new BLL.userbbs().GetModel(id);
                if (bbsmodel == null)
                {
                    return false;
                }
                Model.users umodel = new BLL.users().GetModel(bbsmodel.uid);

                Model.manager mmodel = new BLL.manager().GetModel(this.shopid);
                if (umodel == null || mmodel == null)
                {
                    return false;
                }
                content = string.Concat("你好,你有一条新的留言,请进入商城查看");
                Model.wx_userweixin weixin = new MxWeiXinPF.BLL.wx_userweixin().GetModel(39);
                if (weixin == null)
                {
                    return false;
                }
                if (!AccessTokenContainer.CheckRegistered(weixin.AppId))
                {
                    AccessTokenContainer.Register(weixin.AppId, weixin.AppSecret);
                }

                WxJsonResult ret = Senparc.Weixin.MP.AdvancedAPIs.CustomApi.SendText(weixin.AppId, umodel.wxOpenId, content);
                if (ret.errcode == 0)
                {
                    return true;
                }
            }
            catch (Exception ex)
            {

            }

            return false;
        }
    }
}